// 入口文件
// 导入Vue
import Vue from 'vue'
//导入vuex
import Vuex from 'vuex'
Vue.use(Vuex) //安装


//按需导入mint-ui
import {Header, Swipe, SwipeItem,Button, Navbar, TabItem,Cell,Loadmore,Switch,TabContainer, TabContainerItem,Search,Lazyload} from 'mint-ui'
Vue.component(Header.name,Header)
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.component(Navbar.name, Navbar); 
Vue.component(TabItem.name, TabItem);
Vue.component(Cell.name, Cell);
Vue.component(Loadmore.name, Loadmore);
Vue.component(Switch.name, Switch);
Vue.component(TabContainer.name, TabContainer);
Vue.component(TabContainerItem.name, TabContainerItem);
Vue.component(Search.name, Search);
Vue.use(Lazyload);
// 导入路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'

//导入mui样式
import './lib/mui/css/mui.min.css'

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.emulateJSON = true;

//导入图片预览组件
import VuePreview from 'vue-preview'
//Vue.use(VuePreview)
//with parameters install
Vue.use(VuePreview, {
  bgOpacity: 0.85,
  tapToClose: true,
})

//全局过滤器
Vue.filter('formatDate', function(value, format='') {
   s= value*1000;
  var date = new Date(s);
  var y = date.getFullYear();
  var m = (date.getMonth()+1).toString().padStart(2, '0');
  var d = date.getDate().toString().padStart(2, '0');
  var h = date.getHours().toString().padStart(2, '0')
  var min = date.getMinutes().toString().padStart(2, '0');
  var s = date.getSeconds().toString().padStart(2, '0');
  if (format.toLowerCase() == 'yy-m-d-h-min') {
    return `${y}-${m}-${d}`
  }
  return `${y}-${m}-${d} ${h}:${min}.${s}`
});

//vuex
var car =JSON.parse(localStorage.getItem('car')) || [];
const store = new Vuex.Store({
  state: {
    count: 0,
    car:car,
  },
  mutations: {
    increment (state) {
      state.count++
    },
    //加入购物车
    addToCar(state,caritem){
       var flag= false;
      state.car.some((item)=>{
        if(item.id==caritem.id){
          item.count += caritem.count;
          return flag=true;
        }
      })
      //console.log(flag)
      if(!flag){
        state.car.push(caritem)
      }
      var o=JSON.stringify(state.car)
      localStorage.setItem('car',o)
    },
    //从购物车中删除
    removeCarItem(state,id){
      var car = state.car;
      car.forEach((item,index)=>{
        if(item.id == id){
          car.splice(index,1);
          return true;
        }
      });
      localStorage.setItem('car',JSON.stringify(car)) //更新本地存储
    },
    //改变购物车中数量
    changCarCount(state,redeceinfo){
      var car =state.car;
      car.forEach((item,index)=>{
        if(item.id == redeceinfo.id){
         item.count +=redeceinfo.num
          return true;
        }
      });
      localStorage.setItem('car',JSON.stringify(car)) //更新本地存储
    },  
  },
  getters:{
    //获取购物车总数
    getCarCount(state){
      var c=0;
      if(state.car.length>0){
        state.car.forEach(item => {
          c +=item.count
        });
      }
      return c;
    },
    getCarItem(state){
      return state.car
    },
    //获取购物车中勾选总数总价
    getCarTotal(state){
      var car= state.car,t={o:0,p:0};
      car.forEach(item=>{
        if(item.isSelceted){//如果勾选则相加
          t.o +=parseInt(item.count);
          t.p +=parseInt(item.price)*parseInt(item.count);
          return true;
        }  
      });
      return t;
    },

  }
})

import app from './app.vue'
let vm = new Vue({
    el:'#app',
    render:e=>e(app),
    router,
    store 
})

// router.beforeEach((to, from, next) => {
//   //
//   if(to.fullPath=='/home'){

//   }
// console.log('beforeEach')
// console.log(to,from)
// //
// next()
// })